<?php
require_once(SRC_PATH."Object/Crawler/Link.php");
class FetcherContent{
	
	private $path;
	private $url;
	private $ttl;
	private $content;
	private $title;
	private $description;
	private $keywords;
	private $paragraphs;
	private $links;
	
	public function __construct(	$url = NULL, 
										$ttl = NULL,
										$path = NULL, 
										$content = NULL, 
										$title = NULL, 
										$description = NULL, 
										$keywords = NULL, 
										$paragraphs = NULL, 
										$links = NULL){
		$this->url = $url;
		$this->ttl = $ttl;
		$this->path = $path;
		$this->content = $content;
		$this->title = $title;
		$this->description = $description;
		$this->keywords = $keywords;
		$this->paragraphs = $paragraphs;
		$this->links = $links;		
	}
	
	public function setUrl($url){
		$this->url = $url;
	}
	
	public function setTtl($ttl){
		$this->ttl = $ttl;
	}
	
	public function setPath($path){
		$this->path = $path;	
	}
	
	public function setContent($content){
		$this->content = $content;
	}
	
	public function setTitle($title){
		$this->title = $title;
	}
	
	public function setDescription($description){
		$this->description = $description;
	}
	
	public function setParagraphs($paragraphs){
		$this->paragraphs = $paragraphs;
	}
	
	public function setKeywords($keywords){
		$this->keywords = $keywords;
	}
	
	public function setLinks($links){
		$this->links = $links;
	}
	
	public function getUrl(){
		return $this->url;
	}
	
	public function getTtt(){
		return $this->ttl;
	}
	
	public function getPath(){
		return $this->path;
	}
	
	public function getContent(){
		return $this->content;
	}
	
	public function getTitle(){
		return $this->title;
	}
	
	public function getDescription(){
		return $this->description;
	}
	
	public function getKeywords(){
		return $this->keywords;
	}
	
	public function getParagraphs(){
		return $this->paragraphs;
	}
	
	public function getLinks(){
		return $this->links;
	}
	
	public function addKeyword($keyword){
		if(!is_array($this->keywords))
			$this->keywords = array();
		array_push($this->keywords, trim($keyword));
	}
	
	public function addLink($link){
		if(!is_array($this->links))
			$this->links = array();
		array_push($this->links, $link);
	}
	
	public function addParagraph($paragraph){
		if(!is_array($this->paragraphs))
			$this->paragraphs = array();
		array_push($this->paragraphs, $paragraph);
	}
	
	public function parseContent(){
		$this->parseTitle();
		$this->parseDescription();
		$this->parseKeywords();
		$this->parseLinks();
		$this->parseParagraphs();
		unset($this->content);
	}
	
	public function parseTitle(){
		$matches = NULL;
		if(preg_match('/<title>(.)*<\/title>/i', $this->content, $matches)){
			if($matches[0]){
				$matches[0] = str_replace('<title>', '', $matches[0]);
				$matches[0] = str_replace('</title>', '', $matches[0]);				
				$this->setTitle(utf8_encode(trim($matches[0])));				
			}
		}
	}
	
	public function parseDescription(){
		$matches = NULL;		
		if(preg_match('/<meta name=\"description\" content=\"[^\"]+/i', $this->content, $matches)){
			if($matches[0]){
				$this->setDescription(utf8_encode(trim(str_ireplace("<meta name=\"description\" content=\"", "", $matches[0]))));		
			}
		}
	}
	
	public function parseKeywords(){
		$matches = NULL;		
		if(preg_match('/<meta name=\"keywords\" content=\"[^\"]+/i', $this->content, $matches)){
			if($matches[0]){				
				$matches = explode(',', trim(str_ireplace("<meta name=\"keywords\" content=\"", "", $matches[0])));
				foreach($matches as $keyword){
					$this->addKeyword(utf8_encode(trim($keyword)));
				}
			}
		}
	}
	
	public function parseLinks(){
		$matches = NULL;
		if(preg_match_all('/<a\s+(?:(?:\w+\s*=\s*)(?:\w+|"[^"]*"|\'[^\']*\'))*?\s*href\s*=\s*(?<url>\w+|"[^"]*"|\'[^\']*\')(?:(?:\s+\w+\s*=\s*)(?:\w+|"[^"]*"|\'[^\']*\'))*?>.+?<\/a>/i', $this->content, $matches)){
			foreach($matches[0] as $match){
				
				if(!preg_match('/(nofollow|javascript:)/i', $match)){
					
					$link = new Link($this->url, $this->ttl - 1);
					
					/* Title Parse */					
					if(preg_match('/title=(\'|")[^\'"]+(\'|")/i', $match, $titleMatch)){
						if($titleMatch[0]){
							$titleClean = trim($titleMatch[0]);
							$link->setTitle(utf8_encode(substr($titleClean, 7, strlen($titleClean) - 8)));
						}
					}
					
					/* Href Parse */
					$location = NULL;
					if(preg_match('/href=(\'|")[^\'"]+(\'|")/i', $match, $hrefMatch)){
						if($hrefMatch[0]){
							$locationClean = trim($hrefMatch[0]);
							$link->setLink(utf8_encode(substr($locationClean, 6, strlen($locationClean) - 7)));
						}
					}
					
					/* Text or Image */
					$cleanAnchor = trim(preg_replace('/<a[^>]*>|<\/a[^>]*>/i', '', $match));
					if($cleanAnchor){
						if(preg_match('/<img[\s\S]+(\/>|\/ >| >)/', $cleanAnchor, $imageMatch)){
							$image = new Image();
							if(preg_match('/width=(\'|")\d+(\'|")/i', $imageMatch[0], $widthMatch))
								$image->setWidth(preg_replace('/[^\d]/', '', $widthMatch[0]));
							if(preg_match('/height=(\'|")\d+(\'|")/i', $imageMatch[0], $heightMatch))
								$image->setHeight(preg_replace('/[^\d]/', '', $heightMatch[0]));
							if(preg_match('/src=(\'|")[^"\']+(\'|")/i', $imageMatch[0], $srcMatch))
								$image->setLocation(trim(preg_replace('/(src=(\'|")|"|\')/i', '', $srcMatch[0])));
							if(preg_match('/alt=(\'|")[^"\']+(\'|")/i', $imageMatch[0], $altMatch))
								$image->setText(utf8_encode(trim(preg_replace('/(alt=(\'|")|"|\')/i', '', strip_tags($altMatch[0])))));
							$link->setImage($image);
						}else{
							$link->setText(utf8_encode($cleanAnchor));
						}
					}
					
					$this->addLink($link);
				}
			}			
		}
	}
	
	public function parseParagraphs(){
		
	}
}
?>